// let params = {
//     pagenum: 1,
//     pagesize: 2,
// };
let config = {
    headers: {
        Authorization: localStorage.getItem('token')
    },
    params: {
        pagenum: 1,
        pagesize: 5
    }
}
function renderArticle() {
    axios.get('http://www.itcbc.com:8080/my/article/list',config).then(res => {
        // console.log(res.data);
        let {status,data,total} = res.data;
        if(status === 0) {
            let str = '';
            data.forEach(item => {
                str += `
                <tr>
              <td>${item.title}</td>
              <td>${item.cate_name}</td>
              <td>${item.pub_date}</td>
              <td>${item.state}</td>
              <td>
                <button type="button" class="layui-btn layui-btn-xs">编辑</button>
                <button type="button" class="layui-btn layui-btn-xs layui-btn-danger">删除</button>
              </td>
            </tr>
                `;
            })
            $('tbody').html(str);
            showPage(total)
        }
    })
}
renderArticle();
let laypage = layui.laypage;
function showPage(t) {
    laypage.render({
        elem:'page',
        count:t,
        limit:config.params.pagesize,
        curr:config.params.pagenum,
        limits:[5,10],
        layout:['limit','prev', 'page', 'next','count','skip'],
        jump: function(obj, first){
            //首次不执行
            if(!first){
              //do something
              //obj包含了当前分页的所有参数，比如：
            //   console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
            //   console.log(obj.limit); //得到每页显示的条数
            config.params.pagesize = obj.limit;
            config.params.pagenum = obj.curr;
            renderArticle()
            }
        }
    })
}
axios.get('http://www.itcbc.com:8080/my/category/list',{
    headers: {
        Authorization: localStorage.getItem('token')
    }
}).then(res => {
    let {status,data} = res.data;
    if (status === 0) {
        let str = '';
        data.forEach(item => {
            str += `<option value="${item.id}">${item.name}</option>`;
        })
        $('#category').append(str);
        let form = layui.form;
        form.render('select');
    }
})
$('#search').on('submit',function(e) {
    e.preventDefault();
    let cate_id = $('#category').val();
    let state = $('#state').val();
    if (cate_id) {
        config.params.cate_id = cate_id;
    } else {
        delete config.params.cate_id;
    }
    if (state) {
        config.params.state = state;
    } else {
        delete config.params.state
    }
    config.params.pagenum = 1;
    renderArticle()
})